package com.glory.hzlionpda.adapter;

import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.hzlionpda.R;
import com.glory.hzlionpda.bean.DefectCodeBean;
import com.glory.hzlionpda.bean.WaferBean;
import com.glory.hzlionpda.bean.WaferLotBean;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @date 2017/9/11
 * @desc
 */

public class DefectInputAdapter extends BaseQuickAdapter<DefectCodeBean, BaseViewHolder> {
    private List<WaferLotBean> waferLotBeanList;
    private FragmentManager fragmentManager;

    public DefectInputAdapter(@Nullable List<DefectCodeBean> data) {
        super(R.layout.adapter_defect_input, data);
    }

    public void setData(List<WaferLotBean> waferLotBeanList, FragmentManager fragmentManager) {
        this.waferLotBeanList = waferLotBeanList;
        this.fragmentManager = fragmentManager;
    }


    @Override
    protected void convert(BaseViewHolder helper, final DefectCodeBean item) {
        helper.setText(R.id.tvType, item.getTYPE() + "/" + item.getTYPENAME());
        helper.setText(R.id.tvDefectCode, item.getREASONCODE());
        helper.setText(R.id.tvDefectDesc, item.getDATA_1_VALUE());
        helper.setText(R.id.tvUser, item.getRESPONSIBILITY());
        helper.setText(R.id.etLotId, item.getLOTID());
        helper.setText(R.id.etReason, item.getREASONS());
        LinearLayout llDefectWafer = helper.getView(R.id.llDefectWafer);
        TextView tvWafer = helper.getView(R.id.tvWafer);
        tvWafer.setText(item.getUNITLIST());
        llDefectWafer.setOnClickListener(view -> showChooseWafer(item));
        EditText etLotId = helper.getView(R.id.etLotId);
        Editable etLotIdText = etLotId.getText();
        etLotId.setSelection(etLotIdText.length());
        setLotEditListener(etLotId, item);

        EditText etReason = helper.getView(R.id.etReason);
        Editable etReasonText = etReason.getText();
        etReason.setSelection(etReasonText.length());
        setReasonEditListener(etReason, item);
    }

    private void showChooseWafer(DefectCodeBean item) {
        final FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_choose_wafer, null);
        RecyclerView rvWaferList = view.findViewById(R.id.rvWaferList);
        CheckBox cbCheckAll = view.findViewById(R.id.cbCheckAll);
        rvWaferList.setLayoutManager(new LinearLayoutManager(mContext));
        rvWaferList.setItemAnimator(new DefaultItemAnimator());
        rvWaferList.addItemDecoration(new DividerItemDecoration(mContext, 1));
        List<WaferBean> waferBeanList  = new ArrayList<>();
        for (WaferLotBean waferLotBean : waferLotBeanList){
            if (waferLotBean.getLOTID().equals(item.getLOTID())){
                waferBeanList.addAll(waferLotBean.getWAFER());
                break;
            }
        }
        for (WaferBean waferBean : waferBeanList) {
            waferBean.setSelect(false);
        }
        ChooseWaferListAdapter chooseWaferListAdapter = new ChooseWaferListAdapter(waferBeanList);
        chooseWaferListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        chooseWaferListAdapter.isFirstOnly(true);
        rvWaferList.setAdapter(chooseWaferListAdapter);
        cbCheckAll.setOnCheckedChangeListener((compoundButton, isChecked) -> chooseWaferListAdapter.setAll(isChecked));
        TextView tvConfirm = view.findViewById(R.id.tvConfirm);
        TextView tvCancel = view.findViewById(R.id.tvCancel);
        tvConfirm.setOnClickListener(view12 -> {
            int count = 0;
            StringBuilder stringBuilder = new StringBuilder();
            for (WaferBean waferBean : chooseWaferListAdapter.getData()) {
                if (waferBean.isSelect()) {
                    count++;
                    stringBuilder.append(waferBean.getWAFERID());
                    stringBuilder.append(",");
                }
            }
            String str = stringBuilder.toString();
            if (count > 0) {
                freeUI_generalFragmentDialog.dismiss();
                item.setUNITLIST(str.substring(0, str.length() - 1));
                item.setTRANSQTY1(String.valueOf(count));
                notifyDataSetChanged();
            } else {
                FreeApi_ToastUtils.showFreeToast("未选择片号", mContext, false,
                        Toast.LENGTH_SHORT);
            }
        });
        tvCancel.setOnClickListener(view1 -> freeUI_generalFragmentDialog.dismiss());
        freeUI_generalFragmentDialog.setLayout(view);
        freeUI_generalFragmentDialog.show(fragmentManager, "choose_wafer_dialog");
    }


    private void setLotEditListener(EditText editText, final DefectCodeBean item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setLOTID(s.toString());
            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }

    private void setReasonEditListener(EditText editText, final DefectCodeBean item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setREASONS(s.toString());
            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
